package cn.tedu.ivos.user.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.base.util.PasswordUtil;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos.user.pojo.entity.User;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service//表明当前类为业务层实现类,交给Spring管理并被识别到,方便注入
public class UserServiceImpl implements UserService {
    @Override
    public void saveUser(UserSaveParam userSaveParam) {
        log.debug("用户保存的业务：userSaveParam={}", userSaveParam);
        User user = new User();
        BeanUtils.copyProperties(userSaveParam,user);
        if(user.getId()==null){//新增
            user.setPassword("123456");
            user.setCreateTime(new Date());
            userMapper.insert(user);
        }else{//更新
            user.setUpdateTime(new Date());
            userMapper.update(user);
        }
    }

    @Override
    public void resetPassword(Long userId) {
        log.debug("重置密码的业务：userId={}", userId);
        User user = new User();
        user.setId(userId);
//        user.setPassword("root");
        user.setPassword(PasswordUtil.generateRandomPassword(8));
        log.debug("新密码：user={}", user);
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void changeStatus(Long userId, String status) {
        log.debug("修改状态的业务：userId={},status={}", userId, status);
        User user = new User();
        user.setId(userId);
        user.setStatus(status);
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void deleteUser(Long userId) {
        log.debug("删除用户的业务：userId={}", userId);
        userMapper.deleteUser(userId);
    }

    @Override
    public List<UserVO> selectAuditUserList(Long parentId) {
        log.debug("查询待审核的业务：parentId={}", parentId);
        ArrayList<UserVO> arrayList = new ArrayList<>();
        UserVO auditUser1 =userMapper.selectById(parentId);
        arrayList.add(auditUser1);
        if(auditUser1 !=null && auditUser1.getParentId()!=null){
            UserVO auditUser2 =userMapper.selectById(auditUser1.getParentId());
            arrayList.add(auditUser2);
        }
        return arrayList;
    }


    @Autowired
    UserMapper userMapper;
    @Override
    public UserVO login(UserLoginParam userLoginParam) {
        log.debug("用户登录的业务：userLoginParam={}", userLoginParam);
        UserVO userVO =userMapper.selectByUsername(userLoginParam.getUsername());
        if(userVO==null){
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if(!userVO.getPassword().equals(userLoginParam.getPassword())){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        log.debug("用户查询结果：userVO={}", userVO);
        return userVO;
    }

    @Override
   public List<UserVO> selectUser(UserQuery userQuery){
        log.debug("用户查询：userQuery={}", userQuery);
        List<UserVO> list =userMapper.selectUser(userQuery);
        return list;
    }


}
